#include "characters/enemies/SpecimenEnemy.hh"
#include "characters/enemies/Enemy.hh"

#include <iostream>
#include <string>
#include <algorithm>

SpecimenEnemy::SpecimenEnemy()
    : Enemy("Out-of-Control Specimen",
            "Gained consciousness due to the demon's curse but cannot leave the specimen room",
            500,              // Health
            500,              // Max Health
            200,              // Attack
            100),             // Defense
      willSelfDestruct(false) // Initialize flag
{
    std::cout << "The " << getName() << " twitches violently in its container!" << std::endl;
}

bool SpecimenEnemy::attack(ICharacter &target)
{
    if (!isAlive())
        return false;

    std::cout << getName() << " lashes out with a grotesque appendage at " << target.getName() << "!" << std::endl;

    unsigned damageDealt = getAttackPower();
    bool targetSurvived = target.takeDamage(damageDealt);

    if (!targetSurvived)
    {
        std::cout << target.getName() << " succumbed to the " << getName() << "'s attack!" << std::endl;
    }
    return targetSurvived;
}

bool SpecimenEnemy::takeDamage(unsigned damage)
{
    if (!isAlive())
        return false;

    unsigned healthBefore = getHealth();

    bool alive = Character::takeDamage(damage);

    if (alive && getHealth() < SELF_DESTRUCT_THRESHOLD && !willSelfDestruct)
    {
        willSelfDestruct = true;
        std::cout << getName() << " begins to swell and shudder violently! It looks unstable!" << std::endl;
    }

    if (!alive)
    {
        if (willSelfDestruct)
        {
            std::cout << "With a final spasm, the " << getName() << " is about to detonate!" << std::endl;
        }
        else
        {
            std::cout << "The " << getName() << " collapses into a pile of goo." << std::endl;
        }
    }
    else if (healthBefore > getHealth())
    {
        std::cout << "Fluid leaks from the damaged " << getName() << "." << std::endl;
    }
    else if (damage > 0)
    {
        std::cout << "The attack glances off the " << getName() << "'s tough hide." << std::endl;
    }

    return alive;
}

bool SpecimenEnemy::isAboutToSelfDestruct() const
{
    return !isAlive() && willSelfDestruct;
}

unsigned SpecimenEnemy::getSelfDestructDamage() const
{
    return 300;
}